; loader.asm
; PRG loader

	.include "vicrom.inc"

	.export cploader

			.segment "BOOT"

;
;	Load & run a basic program
;	Inspired by CBM Filebrowser & comp.sys.cbm
;		

runprg:		; reset Interrupt vectors (do not touch I/O vectors)
			sei
			ldx #$6D
			ldy #$FD
			stx $C3
			sty $C4
			ldy #5
			clc
			jsr $FD5D
			jsr $FDF9
			jsr CINT1	; Initialize I/O  
			cli
			jsr $E45B	; initv Initialize Vectors 
			jsr $E3A4	; initcz Initialize BASIC RAM
			jsr $C644	; scrtch Perform [new] (FORCED) and return
			lda #1		; drive no @ $FF
			ldx $FF
			ldy #0
			jsr SETLFS
			lda $0100 	; filename @ $0100
			ldx #$01
			ldy #$01
			jsr SETNAM
			lda #0		; load
			ldx $2B
			ldy $2C
			jsr LOAD
			bcs loaderr
			stx $2D		; save end address
			sty $2E
			jsr $C659	; CLR
			jsr $C533	; Relinks BASIC Program from and to any address... 
			jmp $C7AE	; BASIC Warm Start (RUN)
			
loaderr:	ldx #$1D
			jmp ($300)

cploader:	ldx #cploader-runprg-1
@cplloop:	lda runprg, x
			sta $033C, x	; to cassette buffer
			dex
			bpl @cplloop
			rts
